package com.scaffold.upload.action;
import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.commons.page.Page;

import com.scaffold.BasicBizAction;
import com.scaffold.upload.entity.FileInfo;
import com.scaffold.upload.service.FileInfoService;
import com.scaffold.upload.service.FileService;

@Controller
public class FileInfoJsonAction extends BasicBizAction{
	@Autowired
	private FileInfoService fileInfoService;
	@Autowired
	private FileService fileService;

	@RequestMapping("/fileInfo/get.json")
	public String get(Long id){
		FileInfo fileInfo = fileInfoService.getFileInfo(id);
		this.checkOwner(fileInfo);
		return this.getSuccessResult(fileInfo);
	}

	@RequestMapping("/fileInfo/delete.json")
	public String delete(Long id, String filePath) throws IOException{
		if(id!=null){
			this.fileService.delete(id);
		}else if(filePath!=null){
			this.fileService.delete(filePath);	
		}else{
			return this.getFailureResult("");
		}
		return this.getSuccessResult();
	}

	@RequestMapping("/fileInfo/select.json")
	public String select(String contentType, String temp, String keyword) {
		Page page = this.getPage(8);
		List<FileInfo> list = fileInfoService.pageQuery(page, contentType, temp, keyword);
		return this.getPageResult(page, list);
	}

	/**
	 * 文件信息列表
	 * @param keyword 查询关键字
	 */
	@RequestMapping("/fileInfo/list.json")
	public String list(String contentType, String keyword){
		Page page = this.getPage(20);
		List<FileInfo> list = fileInfoService.pageQuery(page, contentType, null, keyword);
		return this.getPageResult(page, list);
	}

	/**
	 * 保存文件信息
	 */
	@RequestMapping("/fileInfo/save.json")
	public String save(Long id){
		if(id==null){
			FileInfo fileInfo = new FileInfo();
			this.bindParams(fileInfo);
			this.setOwner(fileInfo);
			this.fileInfoService.save(fileInfo);
		}else{
			FileInfo fileInfo = this.fileInfoService.getFileInfo(id);
			this.checkOwner(fileInfo);
			this.bindParams(fileInfo);
			this.fileInfoService.update(fileInfo);
		}
		return this.getSuccessResult();
	}

}
